package com.item.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.item.pojo.User;
import com.item.service.UserService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Map;

@RestController
public class UserController {

    @Resource
    private UserService userService;



    @PostMapping("/login")
    public Map<String, Object> login(String username, String password, HttpSession session) {
        return userService.login(new User(username, password));
    }

    @PostMapping("/verifyPassword")
    public boolean verifyPassword(String oldPassword, HttpSession session) {
        User user = (User) session.getAttribute("login");
        QueryWrapper<User> data = new QueryWrapper<User>();
        User result = userService.getOne(data.eq("username", user.getUsername()).eq("password", oldPassword));
        return result != null ? true : false;
    }

    @PostMapping("/updatePassword")
    public boolean updatePassword(String newPassword, String phone, HttpSession session) {
        User userData = (User) session.getAttribute("login");
        return userService.saveOrUpdate(new User(userData.getId(), newPassword, phone));
    }
}
